package com.zrrd.yunchmall.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.user.entity.Menu;
import com.zrrd.yunchmall.user.service.IMenuService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 后台菜单表 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "权限服务-菜单模块")
public class MenuController {
    @Autowired
    private IMenuService menuService;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("查询菜单树")
    @GetMapping("/treeList")
    public ResponseResult treeList() {
        List<Menu> treeList = menuService.getTreeList();
        return new ResponseResult(200, "查询成功", treeList);
    }
    @ApiOperation("查询菜单列表")
    @GetMapping("/list/{parentId}")
    public ResponseResult list(@PathVariable("parentId") int parentId, int pageNum, int pageSize) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id", parentId);
        return new ResponseResult(200, "查询成功", menuService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @ApiOperation("菜单是否显示")
    @PostMapping("/updateHidden/{menuId}")
    public ResponseResult updateHidden(@PathVariable("menuId") int menuId, @RequestParam int hidden) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", menuId);
        updateWrapper.set("hidden", hidden);
        menuService.update(updateWrapper);
        return new ResponseResult(200, "修改成功");
    }

    @ApiOperation("修改菜单信息")
    @PostMapping("/update/{menuId}")
    public ResponseResult update(@PathVariable("menuId") int menuId, @RequestBody Menu menu) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", menuId);
        menuService.update(menu, updateWrapper);
        //在redis数据库中将这个缓存删除
        redisTemplate.delete("MenuTree");
        return new ResponseResult(200, "修改成功");
    }

    @ApiOperation("删除菜单")
    @PostMapping("/delete/{menuId}")
    public ResponseResult delete(@PathVariable("menuId") int menuId) {
        menuService.removeById(menuId);
        redisTemplate.delete("MenuTree");
        return new ResponseResult(200, "删除成功");
    }

    @ApiOperation("添加菜单信息")
    @PostMapping("/create")
    public ResponseResult create(@RequestBody Menu menu) {
        menuService.save(menu);
        redisTemplate.delete("MenuTree");
        return new ResponseResult(200, "添加成功");
    }

    @ApiOperation("按主键查询")
    @GetMapping("/{id}")
    public ResponseResult select(@PathVariable int id) {
        return new ResponseResult(200, "查询成功", menuService.getById(id));
    }
}
